import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
    meta: { title: '首页' }
  },
  {
    path: '/classic',
    name: 'Classic',
    component: () => import('@/views/ClassicMode.vue'),
    meta: { title: '经典模式', requiresAuth: true }
  },
  {
    path: '/trpg',
    name: 'TRPG',
    component: () => import('@/views/TRPGMode.vue'),
    meta: { title: 'TRPG模式', requiresAuth: true }
  },
  {
    path: '/editor',
    name: 'ModuleEditor',
    component: () => import('@/views/ModuleEditor.vue'),
    meta: { title: '模组编辑器', requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/Profile.vue'),
    meta: { title: '个人中心', requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { title: '注册' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router
